<?php
require 'config.php';
if (isset($_REQUEST['entity'])) {
	$entity = $_REQUEST['entity'];
} else {
	$entity = Constants::NULL;
	header("Location: index.php");
}
if ($entity != Constants::NULL) { //TODO : plus check entity exist 
		$pageTitle = "MR Motors | " .$entity;
		include 'common/header.php';
//    	$logger->log("Entity : $entity");
	if (Security::isCurrentUserGrantedWith("ROLE_ADMIN")) {
		include "src/$entity/$entity.class.php";
		include "src/$entity/dao.class.php";	
		if (isset($_REQUEST['action'])) {
			$action = $_REQUEST['action'];
		} else {
			$action = $_POST['action'];
		}
//		$logger->log("Action : $action");	
		if (isset($action)) {
			switch ($action) {
				case "new": {
					include "src/$entity/form.php";
					break;
				}
				case "update": {
					if (isset($_REQUEST['id'])){
						$id = $_REQUEST['id'];
						include "src/$entity/formu.php";
					}
					break;
				}
				case "delete": {
					if (isset($_REQUEST['id'])) {
						$strId = $_REQUEST['id'];
						switch ($entity) {
							case "car": {
								CarDao::delete($strId);
								break;
							}case "category": {
								CategoryDao::delete($strId);
								break;
							}case "caroption": {
								CarOptionDao::delete($strId);
								break;
							}	
						}
						header("Location:../$www/index_admin.php?entity=$entity&action=list&msg=infDeleted&id=$strId");
					}
					include "src/$entity/list.php";
					break;
				}
				case "list": {
					include "src/$entity/list.php";
					break;
				}
				
				case "browse": {
					include "src/$entity/browse.php";
					break;
				}
			}
		}
	} else {
		include("common/denied.php");	
	}	
include("common/footer.php");
}
?>